<?php
@session_start();
error_reporting(E_ALL^E_NOTICE^E_WARNING);
require_once 'config.php';//引入配置文件
require_once 'bin.php';//引入数据库链接
class E_info
{

    //流程入口
    public static function __index(){
        //接收群组编号，并简单的识别操作 & 当前关键词位置信息
        $gorupID = (int)$_REQUEST['groupNum'];//接收群组编号
        if( $gorupID <= 99 || $gorupID >= 1000 ){ self::__input( "" );}
        //根据群组编号，定位群信息
        $vbot_Info = M('Vbot')->where( array('groupID' => $gorupID ) )->find();
        if( ! $vbot_Info ){
            self::__input( 0 , "提示：当前群尚未开通机器人代开服务,请群主联系天天客服进行处理【天天六胡抢】" );
        }
        //当前是否存在人数满了的房间,这个时候需要输出 - 但是一次只能输出1条,不能同时多条 - 优先级第一  - 1
        self::__is_full( $vbot_Info );
        //当前是否存在已经打完,需要结算的房间 - 2
        self::__is_settle( $vbot_Info );
        //当前是否存在超时需要进行解散的房间 - 3
        self::__is_timeOut( $vbot_Info );
    }

    //条件：超时10分钟未满的房间需要进行解散操作,本群 + 机器人 + 未开始
    public static function __is_timeOut( $vbot_Info ){
        $time_out = 300;
        $map      = array();
        $map['room_cid']    = $vbot_Info['uid'];
        $map['is_vbot']     = 1;
        $map['room_status'] = 0;//状态为未开始
        $map['vbot_gid']        = $vbot_Info['groupID'];
        $map['room_time_micor'] = array('elt', time() - $time_out ); // 开房时间小于当前时间前10分钟了
        $room_info          = M('Room')->where( $map )->order('room_time_micor asc')->find();
        if( $room_info ){
            if( $room_info['consume'] >= 1 ){
                M('Member')->where( array('uid' => $vbot_Info['uid'] ) )->setInc('score', $room_info['consume'] );//如果当前有扣积分先恢复积分
            }
            M('Room')->where( array('room_id' => $room_info['room_id'] ) )->delete();//删除room表记录
            M( self::__gtn( $room_info['room_id'] ) )->where( array('log_rid' => $room_info['room_id'] ) )->delete();//删除log日志记录
            //输出解散日志信息
            self::__input( 3 , "提示：房间号". $room_info['room_id'] ."因超时人数未满已被自动解散【超时". ($time_out/60) ."分钟】" );
        }
    }

    private static function __gtn( $room_id ){
        return 'Room_log_copy' . ($room_id % 200);
    }

    //条件：本群房主开的，机器人开的，已经结束的牌局，群组编号对应，还没进行过结算
    public static function __is_settle( $vbot_Info ){
        $map  = array();
        $map['room_cid']    = $vbot_Info['uid'];
        $map['is_vbot']     = 1;
        $map['room_status'] = 2;//状态为已结束
        $map['vbot_gid']    = $vbot_Info['groupID'];
        $map['is_settle']   = 0;
        $room_info          = M('Room')->where( $map )->order('room_time_micor asc')->find();
        if( $room_info ){
            $text_msg_1 = "[Coffee][Coffee]结算[Coffee][Coffee]". $room_info['room_id'] ."[Coffee][Coffee]";
            if( $room_info['room_player_1'] != 0 ){
                if( $room_info['player_1_score'] == 0 ){$setlle_string = "平";}else if( $room_info['player_1_score'] < 0 ){$setlle_string = "负";}else{$setlle_string = "胜";}
                $text_msg_2 = "[". $setlle_string ."]" . self::__g_Uname( $room_info['room_player_1'] ) . "：" . $room_info['player_1_score'];
            }
            if( $room_info['room_player_2'] != 0 ){
                if( $room_info['player_2_score'] == 0 ){$setlle_string = "平";}else if( $room_info['player_1_score'] < 0 ){$setlle_string = "负";}else{$setlle_string = "胜";}
                $text_msg_3 = "[". $setlle_string ."]" . self::__g_Uname( $room_info['room_player_2'] ) . "：" . $room_info['player_2_score'];
            }
            if( $room_info['room_player_3'] != 0 ){
                if( $room_info['player_3_score'] == 0 ){$setlle_string = "平";}else if( $room_info['player_3_score'] < 0 ){$setlle_string = "负";}else{$setlle_string = "胜";}
                $text_msg_4 = "[". $setlle_string ."]" . self::__g_Uname( $room_info['room_player_3'] ) . "：" . $room_info['player_3_score'];
            }
            if( $room_info['room_player_4'] != 0 ){
                if( $room_info['player_4_score'] == 0 ){$setlle_string = "平";}else if( $room_info['player_4_score'] < 0 ){$setlle_string = "负";}else{$setlle_string = "胜";}
                $text_msg_5 = "[". $setlle_string ."]" . self::__g_Uname( $room_info['room_player_4'] ) . "：" . $room_info['player_4_score'];
            }
            #.把时间组合进去，并加上当前共玩了几局
            $text_msg_6 = "[Coffee]" . $room_info['room_js'] . "/" . $room_info['room_zjs'] . "[Coffee]" . date('Y-m-d H:i:s') . "[Coffee]";
            #.最终结果输出
            self::__input( 2 , $text_msg_1 , $text_msg_2 , $text_msg_3 , $text_msg_4 , $text_msg_5 , $text_msg_6 );
        }
    }

    //获取用户名 - 这里需要把竖线替换掉
    public static function __g_Uname( $uid ){
        $uname = urldecode( M('Member')->where( array('uid' => $uid ) )->getField('name') );
        $uname = str_replace("|","", $uname );
        return $uname;
    }

    //条件：本群房主开的，机器人开的，正在进行中，群组编号对应，还没进行过通知
    public static function __is_full( $vbot_Info ){
        $map  = array();
        $map['room_cid']    = $vbot_Info['uid'];
        $map['is_vbot']     = 1;
        $map['room_status'] = 1;
        $map['vbot_gid']    = $vbot_Info['groupID'];
        $map['full_msg']    = 0;
        $room_info          = M('Room')->where( $map )->order('room_time_micor asc')->find();
        if( $room_info ){
            $uname = urldecode($room_info['uname']);
            $uname = str_replace("|","", $uname );
            M('Room')->where( $map )->order('room_time_micor asc')->data( array('full_msg' => 1) )->save();
            self::__input( 1 , "速度：房间" . $room_info['room_id'] . "人数已满" , $uname );
        }
    }

    public static function __input( $mode = 0 , $text_msg_1 = "" , $text_msg_2 = "" , $text_msg_3 = "" , $text_msg_4 = "", $text_msg_5 = "" , $text_msg_6 = "" ){
        if( $text_msg_1 == "" ){ echo "";exit; }
        $data = array();
        $data['status'] = 0;
        $data['mode']   = $mode;
        $data['text_1'] = $text_msg_1;
        $data['text_2'] = $text_msg_2;
        $data['text_3'] = $text_msg_3;
        $data['text_4'] = $text_msg_4;
        $data['text_5'] = $text_msg_5;
        $data['text_6'] = $text_msg_6;
        $inputText = "";
        foreach ( $data as $key => $value){
            $inputText = $inputText . $value . "|";
        }
        echo $inputText;exit;
    }
}
E_info::__index();